<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use DB;
use Illuminate\Support\Facades\Storage;
class FrontController extends Controller
{
    //
    public function cssSelect(){
        return view('Index.front.cssSelect');
    }

    //定时器
    public function setInterval(){
        return view('Index.front.setInterval');
    }
    public function setIntervalAjax(){
        $number = $_GET['number'];
        return $number;
    }
    //ajax post 提交数据
    public function postTest(){
        if(request()->ajax()){
            $input = request()->all();
            var_dump($input);die;
        }
        return view('Index.front.postTest');
    }
    //定时刷新页面
    public function kline(){
        return view('Index.front.kline');
    }
    public function getKline(){
        $row = DB::table('test')->where('state',0)->first();
        if(!empty($row)){
            DB::table('test')->where('id',$row->id)->update(['state' => 1]);
            return json_encode($row);
        }else{
            return json_encode(['code' => 0]);
        }
    }
    //二级选择框
    public function twoSelect(){
        $first = DB::table('district')->where('type',1)->get();
        return view("Index.front.twoSelect",['first' => $first]);
    }
    public function postTwoSelect(){
        if(request()->ajax()){
            $pid = request()->pid;
            $data = DB::table('district')->where('pid',$pid)->get();
            return $data;
        }
    }
    //三级联动
    public function threeSelect(){
        $first = DB::table('district')->where('type',1)->get();
//        $second = DB::table('district')->where('type',2)->get();
//        $third = DB::table('district')->where('type',3)->get();
        return view("Index.front.threeSelect",['first' => $first]);
    }
    public function postThreeSelect(){
        if(request()->ajax()){
            $pid = request()->pid;
            $data = DB::table('district')->where('pid',$pid)->get();
            return $data;
        }
    }
    //无限极
    public function manySelect(){
        return view("Index.front.manySelect");
    }

    //form 提交表单数据
    public function formPost(Request $request){
        if(request()->isMethod('post')){
            $input = request()->all();
            $this->validate($request, [
                'username' => 'required|min:2|max:20',
                'age' => 'required|integer',
            ],[
                'required' => ':attribute 为必填项',
                'min' => ':attribute 长度不能小于2个字符',
                'max' => ':attribute 长度不能大于20个字符',
                'integer' => ':attribute 必须为数字',
            ],[
                'username' => '用户名',
                'age' => '年龄',
            ]);
            echo '<pre>';
            var_dump($input);die;
        }
        return view("Index.front.formPost");
    }

    public function file(Request $request){
        if(request()->isMethod('post')){
            //普通文件上传需要开启 extension=php_fileinfo.dll
            $file = $request->file('image')->store('images');
        }
        return view("Index.front.fileCos");
    }

    public function fileCos(Request $request){
        if(request()->isMethod('post')){
            $file = $request->file('image');
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg
            echo '<pre>';
            var_dump($originalName,$ext,$realPath,$type);
        }
        return view("Index.front.fileCos");
    }


    //秒杀
    public function secKill(Request $request){
        if($request->isMethod('post')){
            $number = $request->number;
            $length = Redis::llen('ms3');
//            var_dump(Redis::lrange('ms3',0,-1));die;
            if($length>10) return '秒杀活动已经结束';
            for ($i =0;$i<=$number;$i++){
                Redis::lpush('ms3',$i);
                if(Redis::llen('ms3')>10)break;
            }
            return "秒杀成功";
        }
        return view("Index.front.secKill");
    }
    //redis 订阅
    public function creatSub(Request $request){
        if($request->isMethod('post')){
            $strChannel = 'Test_bihu_channel';
            $descr = $request->descr;
            $res = Redis::publish($strChannel, $descr);
            return $res;
        }
        return view("Index.front.creatSub");
    }
    public function receivePub(){
        $strChannel = 'Test_bihu_channel';
        Redis::subscribe(array($strChannel),function($message){
            var_dump($message);
        });
    }

}
